import Vue from 'vue'
import store from './store'
import App from './App'
import graceChecker from './js_sdk/graceui-dataChecker/graceChecker'
import config from './config'
import request from './js_sdk/request/request'
import requestUrl from './js_sdk/request/requestUrl'
import util from './js_sdk/util'
import moment from './js_sdk/moment'
import share from './share'
import Vuex from 'vuex'
import vusLayerInit from './vusui-layer.js';
import vusLayer from './vusui-layer.vue';
Vue.use(vusLayerInit);
Vue.component('vus-layer', vusLayer);
Vue.use(Vuex)
Vue.mixin(share)
// 全局配置
request.setConfig({
	baseUrl: config.request.baseUrl,
	header: {
		'clientId': config.request.clientId,
		'Authorization': '',
		'TENANT-ID': config.request.tenantId,
		'content-type': 'application/json'
	}
})
// 设置请求拦截器
request.interceptors.request(config => {
	if (config.loading) {
		uni.showLoading({
			title: '加载中...'
		});
	}
	return config; // 返回修改后的配置，如未修改也需添加这行
})


// 挂载到全局vue实例上，在页面中可以使用this.$request调用request实例下相应方法
Vue.prototype.$request = request;

// 挂载到全局vue实例上，在页面中可以使用this.$requestUrl调用requestUrl实例下相应方法
Vue.prototype.$requestUrl = requestUrl;

/**
 *  因工具函数属于公司资产, 所以直接在Vue实例挂载几个常用的函数
 *  所有测试用数据均存放于根目录json.js
 *  
 *  css部分使用了App.vue下的全局样式和iconfont图标，有需要图标库的可以留言。
 *  示例使用了uni.scss下的变量, 除变量外已尽量移除特有语法,可直接替换为其他预处理器使用
 */
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}




Vue.prototype.$fire = new Vue();
Vue.prototype.$store = store;
Vue.prototype.$util = util
Vue.prototype.$moment = moment
Vue.prototype.$graceChecker = graceChecker;
Vue.prototype.$api = {
	msg,
	prePage
};

Vue.mixin({
	methods: {
		setData: function(obj, callback) {
			let that = this;
			const handleData = (tepData, tepKey, afterKey) => {
				tepKey = tepKey.split('.');
				tepKey.forEach(item => {
					if (tepData[item] === null || tepData[item] === undefined) {
						let reg = /^[0-9]+$/;
						tepData[item] = reg.test(afterKey) ? [] : {};
						tepData = tepData[item];
					} else {
						tepData = tepData[item];
					}
				});
				return tepData;
			};
			const isFn = function(value) {
				return typeof value == 'function' || false;
			};
			Object.keys(obj).forEach(function(key) {
				let val = obj[key];
				key = key.replace(/\]/g, '').replace(/\[/g, '.');
				let front, after;
				let index_after = key.lastIndexOf('.');
				if (index_after != -1) {
					after = key.slice(index_after + 1);
					front = handleData(that, key.slice(0, index_after), after);
				} else {
					after = key;
					front = that;
				}
				if (front.$data && front.$data[after] === undefined) {
					Object.defineProperty(front, after, {
						get() {
							return front.$data[after];
						},
						set(newValue) {
							front.$data[after] = newValue;
							that.$forceUpdate();
						},
						enumerable: true,
						configurable: true
					});
					front[after] = val;
				} else {
					that.$set(front, after, val);
				}
			});
			// this.$forceUpdate();
			isFn(callback) && this.$nextTick(callback);
		}
	}
});

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
